package com.lgCore.util;/**
 * Created by by on 2020/11/7 14:19
 */

import com.google.common.collect.Maps;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * @program: sxreader_rpc
 * @author: ligang
 * @create: 2020-11-07 14:19
 */
public class ZkUtil {
    private static final Logger log = LoggerFactory.getLogger(ZkUtil.class);

    /**
     * 是否存在 true存在
     *
     * @param client
     * @param nodePath
     * @return
     */
    public static boolean isExists(CuratorFramework client, String nodePath) throws Exception {
        Stat stat = client.checkExists().forPath(nodePath);
        if (null != stat) {
            log.warn("configuration item {} exists!", nodePath);
            return true;
        }
        return false;
    }

    /**
     * 添加
     *
     * @param client
     * @param nodePath
     * @param value
     * @param createMode 节点模式
     *                   PERSISTENT：持久化
     *                   PERSISTENT_SEQUENTIAL：持久化并且带序列号
     *                   EPHEMERAL：临时
     *                   EPHEMERAL_SEQUENTIAL：临时并且带序列号
     * @return
     * @throws Exception
     */
    public static boolean addNode(CuratorFramework client, String nodePath, String value, CreateMode createMode) throws Exception {
        if (!isExists(client, nodePath)) {
            log.info("addNode:::::"+nodePath);
            if (createMode != null) {
                client.create().withMode(createMode).forPath(nodePath, value.getBytes("UTF-8"));
            } else {
                client.create().forPath(nodePath, value.getBytes("UTF-8"));
            }
            return true;
        }
        return false;
    }
    /**
     * 添加
     *
     * @param client
     * @param nodePath
     * @param value
     * @param createMode 节点模式
     *                   PERSISTENT：持久化
     *                   PERSISTENT_SEQUENTIAL：持久化并且带序列号
     *                   EPHEMERAL：临时
     *                   EPHEMERAL_SEQUENTIAL：临时并且带序列号
     *
     * @return
     * @throws Exception
     */
    /**
     *
     * @param client
     * @param nodePath
     * @param value
     * @param createMode 节点模式
     *                   PERSISTENT：持久化
     *                   PERSISTENT_SEQUENTIAL：持久化并且带序列号
     *                   EPHEMERAL：临时
     *                   EPHEMERAL_SEQUENTIAL：临时并且带序列号
     * @param existsAdd 存在则删除再添加
     * @return
     * @throws Exception
     */
    public static boolean addNode(CuratorFramework client, String nodePath, String value, CreateMode createMode,boolean existsAdd) throws Exception {
        if (existsAdd && !isExists(client, nodePath)) {
            delNode(client, nodePath);
        }
        return addNode(client,nodePath,value,createMode);
    }

    /**
     * 修改
     *
     * @param client
     * @param nodePath
     * @param value
     * @return
     * @throws Exception
     */
    public static boolean updateNode(CuratorFramework client, String nodePath, String value) throws Exception {
        if (isExists(client, nodePath)) {
            client.setData().forPath(nodePath, value.getBytes("UTF-8"));
            return true;
        } else {
            log.warn("update configuration item {} failed,item ZNode has not been found!", nodePath);
            return false;
        }
    }


    /**
     * 删除
     *
     * @param client
     * @param nodePath
     * @return
     * @throws Exception
     */
    public static boolean delNode(CuratorFramework client, String nodePath) throws Exception {
        if (isExists(client, nodePath)) {
            client.delete().forPath(nodePath);
            return true;
        } else {
            log.warn("del configuration item {} failed,item ZNode has not been found!", nodePath);
            return false;
        }
    }

    /**
     * 获取节点下所有节点
     *
     * @param client
     * @param nodePath
     * @return
     * @throws Exception
     */
    public static Map<String, String> getAllNodes(CuratorFramework client, String nodePath) throws Exception {
        try {
            List<String> childPaths = client.getChildren().forPath(nodePath);
            Map<String, String> configs = Maps.newHashMap();
            if (null != childPaths && !childPaths.isEmpty()) {
                for (String childPath : childPaths) {
                    String key = childPath.substring(childPath.indexOf("/") + 1);
                    String value = new String(client.getData().forPath(nodePath + "/" + childPath), "UTF-8");
                    configs.put(key, value);
                }
            }
            return configs;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

}
